#!/bin/bash

#Determine the Linux distribution
DISTRO=$(cat /etc/*-release | grep -oP '(Debian|Red Hat|openSUSE|Arch)')

#Check if Wget is installed
if command -v wget &> /dev/null; then
  echo "Wget is already installed"
else
  echo "Wget is not installed, installing..."
  if [ "$DISTRO" == "Debian" ]; then
    sudo apt-get update && sudo apt-get install -y wget
  elif [ "$DISTRO" == "Red Hat" ]; then
    sudo yum install -y wget
  elif [ "$DISTRO" == "openSUSE" ]; then
    sudo zypper install -y wget
  elif [ "$DISTRO" == "Arch" ]; then
    sudo pacman -S wget
  fi
fi

#Check if Git is installed
if command -v git &> /dev/null; then
  echo "Git is already installed"
else
  echo "Git is not installed, installing..."
  if [ "$DISTRO" == "Debian" ]; then
    sudo apt-get update && sudo apt-get install -y git
  elif [ "$DISTRO" == "Red Hat" ]; then
    sudo yum install -y git
  elif [ "$DISTRO" == "openSUSE" ]; then
    sudo zypper install -y git
  elif [ "$DISTRO" == "Arch" ]; then
    sudo pacman -S git
  fi
fi

#Check if Python 3 is installed
if command -v python3 &> /dev/null; then
  echo "Python 3 is already installed"
else
  echo "Python 3 is not installed, installing..."
  if [ "$DISTRO" == "Debian" ]; then
    sudo apt-get update && sudo apt-get install -y python3
  elif [ "$DISTRO" == "Red Hat" ]; then
    sudo yum install -y python3
  elif [ "$DISTRO" == "openSUSE" ]; then
    sudo zypper install -y python3
  elif [ "$DISTRO" == "Arch" ]; then
    sudo pacman -S python
  fi
fi

#Check if additional packages are installed
if [ "$DISTRO" == "Debian" ]; then
  if command -v python3-venv &> /dev/null; then
    echo "Python 3 venv is already installed"
  else
    echo "Python 3 venv is not installed, installing..."
    sudo apt-get update && sudo apt-get install -y python3-venv
  fi
  if command -v libgl1 &> /dev/null; then
    echo "LibGL 1 is already installed"
  else
    echo "LibGL 1 is not installed, installing..."
    sudo apt-get update && sudo apt-get install -y libgl1
  fi
  if command -v libglib2.0-0 &> /dev/null; then
    echo "LibGlib 2.0 is already installed"
  else
    echo "LibGlib 2.0 is not installed, installing..."
    sudo apt-get update && sudo apt-get install -y libglib2.0-0
  fi
elif [ "$DISTRO" == "Red Hat" ]; then
  if command -v gperftools-libs &> /dev/null; then
    echo "Gperftools libs are already installed"
  else
    echo "Gperftools libs are not installed, installing..."
    sudo yum install -y gperftools-libs
  fi
  if command -v libglvnd-glx &> /dev/null; then
    echo "LibGLVND GLX is already installed"
  else
    echo "LibGLVND GLX is not installed, installing..."
    sudo yum install -y libglvnd-glx
  fi
elif [ "$DISTRO" == "openSUSE" ]; then
  if command -v libtcmalloc4 &> /dev/null; then
    echo "LibTCMalloc 4 is already installed"
  else
    echo "LibTCMalloc 4 is not installed, installing..."
    sudo zypper install -y libtcmalloc4
  fi
  if command -v libglvnd &> /dev/null; then
    echo "LibGLVND is already installed"
  else
    echo "LibGLVND is not installed, installing..."
    sudo zypper install -y libglvnd
  fi
fi


#Run the script in the current directory
curl -fsSL https://ollama.com/install.sh | sh

#下载两个模型到模型库

#Install stable-diffusion-webui
SD_WEBUI_DIR="/opt/stable-diffusion-webui"

#Create the installation directory
sudo mkdir -p $SD_WEBUI_DIR

#Clone the repository
git clone https://github.com/CompVis/stable-diffusion-webui.git $SD_WEBUI_DIR

#Install dependencies
cd $SD_WEBUI_DIR
sudo python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

#Install stable-diffusion-webui
pip install .

#Create a systemd service file
sudo tee /etc/systemd/system/stable-diffusion-webui.service <<EOF
[Unit]
Description=Stable Diffusion WebUI
After=network.target

[Service]
User=$USER
ExecStart=/opt/stable-diffusion-webui/venv/bin/python /opt/stable-diffusion-webui/app.py
Restart=always

[Install]
WantedBy=multi-user.target
EOF

#Reload systemd daemon and start the service
sudo systemctl daemon-reload
sudo systemctl start stable-diffusion-webui
sudo systemctl enable stable-diffusion-webui



